package com.iotimc.attendancesystem.util;

/**
 * Created by gdj on 2016/10/15.
 */
public class HexByteUtil {
    public static byte[] hexStringToBytesBe(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase().replaceFirst("0[x|X]","");
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

        }
        return d;
    }

    public static byte[] hexStringToBytesLe(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase().replaceFirst("0[x|X]","");
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[length-i-1] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    // 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
    static public int isOdd(int num)
    {
        return num & 0x1;
    }
    //Hex字符串转int
    static public int HexToInt(String inHex)
    {
        return Integer.parseInt(inHex, 16);
    }
    //Hex字符串转byte
    static public byte hexToByte(String inHex)
    {
        return (byte) Integer.parseInt(inHex,16);
    }
    //hex字符串转字节数组
    static public byte[] HexToByteArr(String inHex)
    {
        int hexlen = inHex.length();
        byte[] result;
        if (isOdd(hexlen)==1)
        {//奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {//偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2)
        {
            result[j]=hexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }
    //1字节转2个Hex字符
    static public String byte2Hex(Byte inByte)
    {
        return String.format("%02x", inByte).toUpperCase();
    }
    //字节数组转转hex字符串
    static public String byteArrToHex(byte[] inBytArr)
    {
        StringBuilder strBuilder=new StringBuilder();
        int j=inBytArr.length;
        for (int i = 0; i < j; i++)
        {
            strBuilder.append(byte2Hex(inBytArr[i]));
//            strBuilder.append(" ");
        }
        return strBuilder.toString();
    }
    //字节数组转转hex字符串
    static public String ByteArrToHexWithoutSpace(byte[] inBytArr)
    {
        StringBuilder strBuilder=new StringBuilder();
        int j=inBytArr.length;
        for (int i = 0; i < j; i++)
        {
            strBuilder.append(byte2Hex(inBytArr[i]));
        }
        return strBuilder.toString();
    }
    //字节数组转转hex字符串，可选起点，跟长度
    static public String ByteArrToHex(byte[] inBytArr, int offset, int byteCount)
    {
        StringBuilder strBuilder=new StringBuilder();
        int j=byteCount;
        for (int i = offset; i < j; i++)
        {
            strBuilder.append(byte2Hex(inBytArr[i]));
        }
        return strBuilder.toString();
    }
    //short转byte[],低位在前
    public static byte[] shortToBytesLe(short shortValue) {
        byte[] bytes = new byte[2];

        for (int i = 0; i < bytes.length; ++i) {
            bytes[i] = (byte) (shortValue >> i * 8 & 0xFF);
        }

        return bytes;
    }
    public static String shortToHexLe(short shortValue)
    {
        return ByteArrToHexWithoutSpace(shortToBytesLe(shortValue));
    }
    //short转byte[],高位在前
    public static byte[] shortToBytesBe(short shortValue) {
        byte[] bytes = new byte[2];

        for (int i = 0; i < bytes.length; ++i) {
            bytes[(bytes.length - i - 1)] = (byte) (shortValue >> i * 8 & 0xFF);
        }
        return bytes;
    }
    public static String shortToHexBe(short shortValue)
    {
        return ByteArrToHexWithoutSpace(shortToBytesBe(shortValue));
    }
    //byte[]转short,高位在前
    public final static short bytesToshortLe(byte[] b,int offset,int bit)
    {
        if(bit == 1)
            return (short)((b[offset]&0xff));
        else
            return (short)((b[offset]&0xff)|((b[offset+1]&0xff) << 8));
    }
    //byte[]转short,低位在前
    public final static short bytesToshortBe(byte[] b,int offset,int bit)
    {
        if(bit == 1)
            return (short)((b[offset]&0xff));
        else
            return (short)((b[offset]&0xff) << 8|((b[offset+1]&0xff)));
    }

    public static long byte2Long(byte[] bytes, int offsetStart, int end) {
        offsetStart = offsetStart*2;
        end = offsetStart + end*2;
        long result = 0;
        String strs = com.iotimc.comass.util.HexByteUtil.ByteArrToHexWithoutSpace(bytes);
        char[] strArr = strs.substring(offsetStart, end).toCharArray();
        char[] rStrArr = new char[strArr.length];
        for(int i=strArr.length-1; i>0; i=i-2) {
            System.arraycopy(strArr, i-1, rStrArr, rStrArr.length-1-i , 2);
        }

        result = Long.parseLong(String.valueOf(rStrArr), 16);
        return result;
    }
}
